package com.pilipili.zeus.eduService.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduVideo;
import com.pilipili.zeus.eduService.entity.excel.VideoExcelData;
import com.pilipili.zeus.eduService.service.EduVideoService;

public class VideoListener extends AnalysisEventListener<VideoExcelData> {
    public EduVideoService videoService;
    public VideoListener() {

    }

    public VideoListener(EduVideoService videoService) {
        this.videoService = videoService;
    }

    //按行读取Excel内容
    @Override
    public void invoke(VideoExcelData videoExcelData, AnalysisContext analysisContext) {
        if (videoExcelData == null) {
            throw new MyException(20001, "文件数据为空");
        }
        EduVideo eduVideo = this.isExistCourseSort(videoService, videoExcelData.getCourseId(),videoExcelData.getSort());
        if (eduVideo == null) {
            eduVideo = new EduVideo();
            eduVideo.setCourseId(videoExcelData.getCourseId());
            eduVideo.setChapterId(videoExcelData.getChapterId());
            eduVideo.setTitle(videoExcelData.getTitle());
            eduVideo.setVideoSourceId(videoExcelData.getVideoSourceId());
            eduVideo.setVideoOriginalName(videoExcelData.getVideoOriginalName());
            eduVideo.setSort(videoExcelData.getSort());
            videoService.save(eduVideo);

        }
    }
    private EduVideo isExistCourseSort(EduVideoService videoService, String id, Integer sort){
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",id);
        wrapper.eq("sort",sort);
        return videoService.getOne(wrapper);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
